#include "StdAfx.h"
#include "ExporterIGES.h"

#include "ExporterFactory.h"
#include "AnyDocument.h"
#include "AnyView.h"

#include <TDF_ChildIterator.hxx> 
#include <TNaming_Tool.hxx> 
#include <IGESControl_Controller.hxx>
#include <IGESControl_Writer.hxx>
#include <Interface_Static.hxx>

#pragma  comment(lib, "TKIGES.lib")
#pragma  comment(lib, "TKXSBase.lib")
REGISTERE_FILEEXPORTER(CExporterIGES, IGES_EXPORTER_GUID);

CExporterIGES::CExporterIGES(void)
{
	m_ExportInfo.szDescription = _T("STL Files (*.iges)|*.iges||");
	m_ExportInfo.szFileExt = _T(".iges");
}

CExporterIGES::~CExporterIGES(void)
{
}

bool CExporterIGES::Export()
{
	CAnyDocument* pAnyDoc = m_ExportInfo.GetDocument();
	CAnyView* pAnyView = m_ExportInfo.GetView();

	if( NULL == pAnyDoc || NULL == pAnyView)
		return false;

	Handle_TDocStd_Document hDoc = pAnyDoc->GetStdDoc();
	TDF_Label labelRoot = hDoc->Main();

	IGESControl_Controller::Init();
	IGESControl_Writer ICW (Interface_Static::CVal("XSTEP.iges.unit"),
		Interface_Static::IVal("XSTEP.iges.writebrep.mode"));
	 
	int nCount = 0;
	TDF_ChildIterator it(labelRoot);
	for(; it.More(); it.Next(), ++nCount)
	{

		TDF_Label subLabel = it.Value();
		Handle_TNaming_NamedShape  namedShape;
		if(!subLabel.FindAttribute(TNaming_NamedShape::GetID(), namedShape))
			continue;

		TopoDS_Shape shape = TNaming_Tool::GetShape(namedShape);
		if(shape.IsNull())
			continue;

		ICW.AddShape(shape);
	}

	if(nCount == 0)
		return false;

	ICW.ComputeModel();
	Standard_CString szFile = m_ExportInfo.szFullPath.GetBuffer();
	return ICW.Write(szFile ) == Standard_True;
}